package jdklearning.reflect.method;

import java.lang.reflect.Method;


/**
 * https://blog.csdn.net/wenyuan65/article/details/81145900
 * @author shenenlu 2021年03月02日 下午16:24:32
 */
public class MethodInvoke {

    public static void main(String[] args)  throws Exception{
        Method animalMethod = Animal.class.getDeclaredMethod("print", String.class);
        Method iAnimalMethod = IAnimal.class.getDeclaredMethod("print", String.class);
        Method catMethod = Cat.class.getDeclaredMethod("print", String.class);
        Animal animal = new Animal(){

        };

        System.out.println(animalMethod.getDeclaringClass());
        Cat cat = new Cat();
        animalMethod.invoke( cat, "arg");

        iAnimalMethod.invoke(new IAnimal() {
            @Override
            public void print(String arg) {
                System.out.println(arg);
            }
        },"AAA");

        catMethod.invoke(cat , "arg");
        System.out.println(catMethod.getDeclaringClass());
        catMethod.invoke(animal ,"arg");
    }
}

interface IAnimal{
    void print(String arg);
}

abstract class Animal {

    public void print(String arg) {
        System.out.println("Animal.print("+ arg +")");
    }

}

class Cat extends Animal implements IAnimal{

    @Override
    public void print(String arg) {
        System.out.println("Cat.print("+ arg +")");
    }

}
